package com.luoye.lj.platform.controller;

import com.luoye.lj.util.mvc.annotation.OperateLog;
import com.luoye.lj.util.mvc.data.SimpleResponseBody;
import com.luoye.lj.util.mvc.log.DefaultDescriptionGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

/*
 * Created with IntelliJ IDEA.
 * User:
 * Date:
 * Time:
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/system/file/view")
public class FileViewController {
    private Logger logger = LoggerFactory.getLogger(FileViewController.class);

    @Value("${file.save.dir}")
    String fileSaveDir;

    @RequestMapping("/image/{fileName}")
    //@OperateLog(value = "图片预览",descriptionGenerator = DefaultDescriptionGenerator.class)
    public void view(HttpServletRequest request, HttpServletResponse response, @PathVariable("fileName")String fileName){
        logger.debug("fileName->" + fileName);

        //获取Referer
        String referer=request.getHeader("Referer");
        logger.debug("referer->" + referer);
        //读出文件到response输出流
        response.setContentType("image/jpeg");
        String filePath=fileSaveDir+"/"+fileName;
        File file=new File(filePath);
        if(file.exists()){
            logger.debug("write File->" + filePath);
            FileInputStream fileInputStream=null;
            OutputStream outputStream=null;
            try {
                fileInputStream=new FileInputStream(file);
                outputStream=response.getOutputStream();
                byte[] bytes=new byte[1024];
                while (fileInputStream.read(bytes)>0){
                    outputStream.write(bytes);
                }
                return;
            } catch (IOException e) {
                logger.warn(e.getMessage().toString(),e);
            }finally {
                if (fileInputStream!=null){
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        logger.warn(e.getMessage().toString(),e);
                    }
                }
                if(outputStream!=null){
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        logger.warn(e.getMessage().toString(),e);
                    }
                }
            }
        }else {
            try {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/html;charset=UTF-8");
                response.getWriter().println("文件不存在!");
            } catch (IOException e) {
                logger.warn(e.getMessage().toString(), e);
            }
        }
    }

}
